package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
//        List<Department> departments = departmentService.listAll();
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/department/list";
    }

    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null) {
            Department department = departmentService.get(id);
            model.addAttribute("department",department);
        }
        return "/department/input";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department){

        if (department.getId() != null) {
            departmentService.update(department);
        }else {
            departmentService.save(department);
        }
        return new JsonResult();


    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){

            departmentService.delete(id);
            return new JsonResult();



    }
}
